package org.cfx.org.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.cfx.basic.annotation.Permission;
import org.cfx.basic.util.AjaxResult;
import org.cfx.basic.util.PageList;
import org.cfx.org.domain.Department;
import org.cfx.org.query.DepartmentQuery;
import org.cfx.org.service.IDepartmentService;
import org.cfx.org.service.IEmployeeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author RTX 9090
 *
 *
 * RestFul:请求风格
 *      使用http请求类型替换请求路径里面操作的动词，安全性高
 *      @RequestMapping 可以接收多种请求类型 method = RequestMethod.PUT可以接收单一的请求类型
 *      使用RestFul后不会再使用@RequestMapping
 *          @PutMapping-->接收put请求类型
 *          @DeleteMapping
 */
@RestController
@RequestMapping("/department")
@Tag(name = "部门管理",description = "部门的CRUD")
@Permission(name = "部门维护")
public class DepartmentController {
    @Resource
    private IDepartmentService departmentService;

    @Resource
    private IEmployeeService employeeService;

//    @RequestMapping(value = "/saveOrUpdate",method = RequestMethod.PUT)
    @PutMapping
    @Operation(summary = "添加或者修改",description = "需要填写部门信息")
    @Permission(name = "添加或修改")
    public AjaxResult saveOrUpdate(@Parameter(description = "部门信息")
                                       @RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.add(department);
            } else {
                departmentService.update(department);
            }
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("Error saving department");
        }
    }

//    @RequestMapping("/delete/{id}")
    @DeleteMapping("/{id}")
    @Operation(summary = "根据id删除部门")
    @Permission(name = "批量删除部门",desc = "部门删除权限")
    public AjaxResult delete(@Parameter(description = "填部门id就行了")
                                 @PathVariable("id") Long id) {

        try {
            departmentService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("Error");
        }
    }

    @DeleteMapping("/deleteList")
    @Operation(summary = "批量删除部门")
    @Permission(name = "批量删除部门")
    public AjaxResult deleteList() {
        return AjaxResult.error();
    }

//    @RequestMapping("/query/{id}")

    @GetMapping("/{id}")
    @Operation(summary = "根据id查询部门")
    public AjaxResult queryById(@Parameter(description = "填需要查询的部门id就行了")
                                    @PathVariable("id") Long id) {
        return AjaxResult.success(departmentService.queryById(id));
    }


    @GetMapping
    @Operation(summary = "查询所有部门")
    public AjaxResult queryAll() {
        List<Department> departments = departmentService.queryAll();
        return AjaxResult.success(departments);
    }

    @PostMapping
    @Operation(summary = "分页+查询所有部门")
    public AjaxResult queryPage(@RequestBody DepartmentQuery departmentQuery) {
        PageList<Department> departmentPageInfo = departmentService.queryAllPage(departmentQuery);
        return AjaxResult.success(departmentPageInfo);
    }

    @GetMapping("/deptTree")
    @Permission(name = "部门关系",desc = "查询上级")
    public AjaxResult getDeptTree() {
        List<Department> departments = departmentService.queryTree();
        return AjaxResult.success(departments);
    }
}
